package com.megoc.menu.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.megoc.menu.R;
import com.megoc.menu.adapter.FindGridViewAdapter;
import com.megoc.menu.adapter.FindHeaderPagerAdapter;
import com.megoc.menu.adapter.FindListViewAdapter;
import com.megoc.menu.model.find.FindAd;
import com.megoc.menu.model.find.FindList;
import com.megoc.menu.model.find.FindRoot;
import com.megoc.menu.model.find.FindTags;
import com.megoc.menu.utils.OkHttpClientManager;
import com.megoc.menu.view.CircleIndicator;
import com.megoc.menu.view.MenuGridView;
import com.squareup.okhttp.Request;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FindFragment extends Fragment {
    private final static String URL="http://api.haodou.com/mall/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1444148409000&vc=76&vn=5.3.0&loguid=0&deviceid=haodou866938025500939&uuid=91dfbd0dffb6891be09b6f09952643db&channel=huawei_v530&method=idx.index&virtual=&signmethod=md5&v=2&timestamp=1444148607&nonce=0.11879799309318584&appsign=7ae06f7e89b25e854e61639d6267cebe";
    private Gson mGson;
    private ListView mListView;
    private FindListViewAdapter adapter;
    private List<FindList> findLists;
    private FindHeaderPagerAdapter findHeaderPagerAdapter;
    private List<FindAd> findAds;
    private MenuGridView menuGridView;
    private FindGridViewAdapter findGridViewAdapter;
    private List<FindTags> findTags;
    public FindFragment() {
        // Required empty public constructor
        mGson=new Gson();
        findLists=new ArrayList<>();
        findAds=new ArrayList<>();
        findTags=new ArrayList<>();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_find, container, false);
        View headerview=inflater.inflate(R.layout.find_listview_headerview,null);
        final ViewPager viewPager= (ViewPager) headerview.findViewById(R.id.find_listview_header_viewpager);
        final CircleIndicator circleIndicator= (CircleIndicator) headerview.findViewById(R.id.indicator);
        findHeaderPagerAdapter=new FindHeaderPagerAdapter(findAds,getContext());

        mListView= (ListView) view.findViewById(R.id.find_listview);
        menuGridView= (MenuGridView) headerview.findViewById(R.id.find_gridview);
        mListView.addHeaderView(headerview);
        adapter=new FindListViewAdapter(findLists,getContext());
        findGridViewAdapter=new FindGridViewAdapter(findTags,getContext());
        OkHttpClientManager.getAsyn(URL, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                FindRoot findRoot=mGson.fromJson(response,FindRoot.class);

                findLists.clear();
                findLists.addAll(findRoot.getResult().getList());
                mListView.setAdapter(adapter);
                findAds.clear();
                findAds.addAll(findRoot.getResult().getAd());
                viewPager.setAdapter(findHeaderPagerAdapter);
                circleIndicator.setViewPager(viewPager);
                findTags.clear();
                findTags.addAll(findRoot.getResult().getTags());
                menuGridView.setAdapter(findGridViewAdapter);
            }
        });
        return view;
    }


}
